package com.webshop.Dao;

import com.webshop.database.ConnPool;
import com.webshop.database.JDBCUtil;
import com.webshop.domain.user.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class UserDao {
    public static User getUser(String username, String password){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            String sql = "select * from user where username=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while (rs.next()){
                user = new User();
                user.setItemsid(rs.getString("itemsid"));
                user.setUserid(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setBirthday(rs.getString("birthday"));
                user.setEmail(rs.getString("email"));
                user.setTel(rs.getString("tel"));
                user.setSex(rs.getString("sex"));
                user.setState(rs.getString("state"));
                user.setModify(rs.getString("modify"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn,ps,rs);
        }
        return user;
    }
    public static void addUser(User user){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps =  null;
        ResultSet rs = null;
        String sql = "insert into user values (?,?,?,?,?,?,?,?,?,null,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getName());
            ps.setString(4,user.getEmail());
            ps.setString(5,user.getTel());
            ps.setString(6,user.getBirthday());
            ps.setString(7,user.getSex());
            ps.setString(8,user.getState());
            ps.setString(9,user.getModify());
            ps.setString(10,user.getItemsid());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }

    public static int checkUsername(String username){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int flag = 1;
        String sql = "select * from user where username=?";
        conn = ConnPool.getConn();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            if(rs.next()){
                flag = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return flag;
        }
    }
    public static void updateUesr(User user){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "update user set name=? , email=? , tel=? , birthday=? , itemsid=? where userid="+user.getUserid();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getEmail());
            ps.setString(3,user.getTel());
            ps.setString(4,user.getBirthday());
            ps.setString(5,user.getItemsid());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public static String[] getItemsId(User user){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String[] itemsids = null;
        String sql = "select itemsid from user where userid=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,String.valueOf(user.getUserid()));
            rs = ps.executeQuery();
            String itemsid = null;
            while (rs.next()){
                itemsid = rs.getString("itemsid");
            }
            if (itemsid !=null){
                itemsids = itemsid.split("#");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return itemsids;
        }
    }

    public  static  User getUser(int id){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            String sql = "select * from user where userid=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,String.valueOf(id));
            rs = ps.executeQuery();
            while (rs.next()){
                user = new User();
                user.setItemsid(rs.getString("itemsid"));
                user.setUserid(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setBirthday(rs.getString("birthday"));
                user.setEmail(rs.getString("email"));
                user.setTel(rs.getString("tel"));
                user.setSex(rs.getString("sex"));
                user.setState(rs.getString("state"));
                user.setModify(rs.getString("modify"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn,ps,rs);
        }
        return user;
    }



//    public static void main(String[] args) {
//        User user = UserDao.getUser("133237430","123456");
//
//    }
}
